<?php defined('SYSPATH') or die('No direct script access.');

class Model_Stat
{ 
    public static function update_suppliers_views($suppliers)
    {
        $ids = array();
        foreach ($suppliers as $supplier) {
            $ids[] = $supplier->id;   
        }
        if (count($ids)) {
            DB::update('supplier_stats')
                    ->set(array('views' => DB::expr('views + 1')))
                    ->where('supplier_id', 'IN', $ids)
                    ->execute();   
        }
        //DB::select()->from('words')->where('word','IN',$words)->and_where('kei','>', 0)->execute()->as_array();
    }
    
    public static function update_supplier_rank($supplier, $rank = 1)
    {
        $supplier_id = $supplier;
        if ($supplier instanceof Model_Supplier) {
            $supplier_id = $supplier->id;   
        }
        
        DB::update('supplier_stats')
                    ->set(array('rank' => DB::expr('rank + '.$rank)))
                    ->where('supplier_id', '=', $supplier_id)
                    ->execute();     
    }
    
    public static function update_goods_views($goods)
    {
        $ids = array();
        foreach ($goods as $good) {
            $ids[] = $good->id;   
        }
        if (count($ids)) {
            DB::update('good_stats')
                    ->set(array('views' => DB::expr('views + 1')))
                    ->where('good_id', 'IN', $ids)
                    ->execute();   
        }
    }
    
    public static function update_good_rank($good, $rank = 1)
    {
        $good_id = $good;
        if ($good instanceof Model_Good) {
            $good_id = $good->id;   
        }
        
        DB::update('good_stats')
                    ->set(array('rank' => DB::expr('rank + '.$rank)))
                    ->where('good_id', '=', $good_id)
                    ->execute();     
    }
}